package cn.bugstack.domain.order.service;


/**
 * @author 丨一
 * @version 1.0
 * @title OrderService
 * @description:
 * @date 2024/11/12 20:17
 */

import cn.bugstack.domain.order.adapter.port.IProductPort;
import cn.bugstack.domain.order.adapter.repository.IOrderRepository;
import cn.bugstack.domain.order.model.aggregate.CreateOrderAggregate;
import cn.bugstack.domain.order.model.entity.PayOrderEntity;
import cn.bugstack.domain.order.model.valueOBJ.OrderStatusVO;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Slf4j
@Service
public class OrderService extends AbstractOrderService {
    
    
    @Value("${alipay.notify_url}")
    private String notify_url;
    
    @Value("${alipay.return_url}")
    private String return_url;
    
//    @Resource
//    private AlipayClient alipayClient;
    
    

    public OrderService(IOrderRepository repository, IProductPort port) {
        super(repository, port);
    }

    @Override
    protected PayOrderEntity doPayOrder(CreateOrderAggregate createOrderAggregate) throws AlipayApiException {
//        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();  // 发送请求的 Request类
//        request.setNotifyUrl(notify_url);
//        request.setReturnUrl(return_url);
//
//        JSONObject bizContent = new JSONObject();
//        bizContent.put("out_trade_no", createOrderAggregate.getOrderEntity().getOrderId());  // 我们自己生成的订单编号
//        bizContent.put("total_amount", createOrderAggregate.getOrderEntity().getTotalAmount()); // 订单的总金额
//        bizContent.put("subject", createOrderAggregate.getProductEntity().getProductName());   // 支付的名称
//        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");  // 固定配置
//        request.setBizContent(bizContent.toString());
//
//        //支付 url 的地址
//        String form = alipayClient.pageExecute(request).getBody();
//        PayOrderEntity payOrderEntity = PayOrderEntity.builder()
//                .orderId(createOrderAggregate.getOrderEntity().getOrderId())
//                .payUrl(form)
//                .orderStatusEnum(OrderStatusVO.PAY_WAIT)
//                .build();
//        super.repository.updateOrderPayInfo(payOrderEntity);
//        return payOrderEntity;
        return null;
    }

    @Override
    protected void doSaveOrder(CreateOrderAggregate createOrderAggregate) {
        super.repository.saveOrder(createOrderAggregate);
    }
}
